/*******************************************************************************
 *  Copyright (c) 2017 Oliver Meili
 *  All rights reserved. This program and the accompanying materials
 *  are made available under the terms of the Eclipse Public License v1.0
 *  and Eclipse Distribution License v1.0 which accompany this distribution.
 *   
 *  The Eclipse Public License is available at
 *  http://www.eclipse.org/legal/epl-v10.html
 *  The Eclipse Distribution License is available at
 *  http://www.eclipse.org/org/documents/edl-v10.php.
 *   
 *  Contributors:
 *  Oliver Meili <omi@ieee.org>
 *******************************************************************************/
package org.eclipse.vorto.codegen.ble.alpwise.templates

import org.eclipse.vorto.core.api.model.informationmodel.InformationModel
import org.eclipse.vorto.codegen.api.InvocationContext
import org.eclipse.vorto.codegen.ble.model.BleInvocationContext
import org.eclipse.vorto.codegen.ble.templates.BleGattTemplate

class AlpwiseAppSourceTemplate extends BleGattTemplate<InformationModel> {
	
	override getFileName(InformationModel model) {
		return "BleApp.c";
	}
	
	override getPath(InformationModel model) {
		return rootPath
	}
	
	override getContent(InformationModel model, InvocationContext context) {
'''
/* Template generated by Vorto */

/* system header files */
#include <stdio.h>

«FOR fb : model.properties»
	«IF context.getMappedElement(fb.type, "Service").hasAttribute("uuid")»
		#include "«fb.type.name»/«fb.type.name».h"
	«ENDIF»
«ENDFOR»
«FOR service : (context as BleInvocationContext).device.services»
	#include "services/«service.name»Service.h"
«ENDFOR»

void BleApp_ServicesInit(void)
{
	«FOR service : (context as BleInvocationContext).device.services»
		«service.name»Service_Init();
	«ENDFOR»
}

«FOR fb : model.properties»
	«FOR status : fb.type.functionblock.status.properties»
		«IF context.getMappedElement(status, "source").hasAttribute("uuid")»
			void «fb.type.name»_Cbk_«status.name.toFirstUpper»(«fb.type.name»_t *«fb.type.name.toLowerCase»)
			{
				«context.getMappedElement(status, "source").getAttributeValue("datatype", "uint16")»_t value;
				
				/* ENTER YOUR CODE TO READ SENSOR DATA HERE */
				value = 0;
				
				«fb.type.name»_Set«status.name.toFirstUpper»Value(«fb.type.name.toLowerCase», value);
			}
			
		«ENDIF»
	«ENDFOR»
«ENDFOR»

void BleApp_SensorsInit(void)
{
	/* ENTER YOUR SENSOR INITIALIZATION CODE HERE */
}
'''
	}		
}